<template>
  <div class="reportMistakeInfoAction el-card is-always-shadow el-card__body">
    <el-dialog title="预览" :visible="dialogVisible" :gutter="24" @close="dialogVisible=false" :modal-append-to-body="false" width="60%">
      <el-form>
        <el-form-item label="收入日期">
            <el-date-picker type="date" placeholder="选择开始日期" v-model="searchForm.startTime" value-format="yyyy-MM-dd" style="width:200px" :disabled="true"></el-date-picker>
         ~
            <el-date-picker type="date" placeholder="选择结束时间" v-model="searchForm.endTime" value-format="yyyy-MM-dd" style="width:200px" :disabled="true"></el-date-picker>
        </el-form-item>
      </el-form>
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="listLoading"
      :data="tableData"
      :search.sync="searchForm"
      @search-change="handleFilter"
      @selection-change="selectionChange">

      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" plain @click.stop="preview(scope.row,-1,'预览')">预览</el-button>
      </template>
    </avue-crud>
<!--    <report-mistake-info-editor ref="reportMistakeInfoEditor" v-if="isShowReportMistakeInfoEditor"></report-mistake-info-editor>-->
      <div style="width: 100%;text-align: center;margin-top: 30px">
        <el-button type="primary" @click="$refs.checkAccount.dialogVisibleLongPeriod=true">确认</el-button>
        <el-button type="primary" :disabled="!allowClickSubmit" :loading="loadingShow" @click="submitBomData">提交</el-button>
        <el-button @click="dialogVisible=false">取消</el-button>
      </div>
<!--      密码确认-->
<checkAccount ref="checkAccount" :params="{roleCode:'ZBZZ',isCheckRole:true}" @checkSubmitSuccess="checkSubmitSuccess"></checkAccount>
    </el-dialog>
  </div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import ExportJsonExcel from "js-export-excel";
    import moment from 'moment';
    import checkAccount from '../checkAccount.vue';
    import {exportDataToExcel} from "../../../../api/subwayRat/exportSelf";
    import {
      longZQJSOptionList
    } from "../../../../api/stationTiecketManager/shouRuJieSuan/bom/longZQJS";
    import {getPersonRole} from "../../../../api/subwayRat/reportMondify/reportMistakeInfo";
    import {checkAccountPassword} from "../../../../api/subwayRat/commonCheck";
    import {
      bomSubmitTvmData,
      getXw_bom_shouJieSuan_waitSubmit
    } from "../../../../api/stationTiecketManager/shouRuJieSuan/bom/waitSubmitMain";
    import { dateFormat } from "@/util/date";
    export default {
      components:{checkAccount
      },
      data() {
            return {
              dialogVisible : false,
              dialogVisibleLongPeriod:false,
              listLoading: true,
              loadingShow: false,
              fileTemp:{},
              searchForm:{
                startTime:'',
                endTime:''
              },
              allowClickSubmit:false,
              attendancePersonJobNo:'',
              tableData:[],
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              autoWidth: true,
              downloadLoading: false,
              isShowReportMistakeInfoEditor:false,//是否加载编辑页面
              multipleSelectionRows:[],
              multipleSelectionKeys:[],
              accountToken:'',//账号确认后的token
              option:longZQJSOptionList(),
            }
        },
        created() {

        },
        methods: {
          moment,
          search(){
            this.searchForm.longSearch = 1;
            // this.searchForm.lineCode=this.$store.getters.userInfo.lineCode;
            // this.searchForm.stationCode=this.$store.getters.userInfo.stationCode;
            this.searchForm.roleCode='SPY';
            // getPersonRole(this.searchForm).then((res)=>{
            //   this.tableData=res.data.data;
            //   this.listLoading=false;
            // });
            let startTime = new Date(this.searchForm.startTime);
            let endTime = new Date(this.searchForm.endTime);
            if(startTime.getDate()>endTime.getDate()){
              let lastDay = new Date(endTime.setDate(0));
              this.searchForm.endTime = dateFormat(lastDay);
            }
            this.searchForm.lineCode=this.$store.getters.userInfo.lineCode;
            this.searchForm.stationCode=this.$store.getters.userInfo.stationCode;
            this.searchForm.startSettleDate=this.searchForm.startTime;
            //this.searchForm.endSettleDate=this.searchForm.endTime;
            this.searchForm.endSettleDate=this.searchForm.endTime;
            this.searchForm.is_not_settle_status=2; //非等于2的情况
            this.searchForm.approvalStatus=0;
            let now = new Date();
            let future = new Date();
            if (now.getHours() < 6) {
              now.setDate(now.getDate() - 1);
            }else{
              future.setDate(now.getDate() + 1);
            }
            this.searchForm.startTime = dateFormat(now).substr(0, 10);
            this.searchForm.endTime = dateFormat(now).substr(0, 10);
            this.searchForm.startSettleDate=dateFormat(now).substr(0, 10) + " 02:00:00";
            this.searchForm.endSettleDate=dateFormat(now).substr(0, 10) + " 01:59:59";
            if(this.searchForm.startTime && this.searchForm.endTime){
              getXw_bom_shouJieSuan_waitSubmit(this.searchForm).then((res)=>{
                this.tableData=res.data.data;
                this.listLoading = false;
                this.page.total=res.data.total;
              });
            }
          },
          checkSubmitSuccess(res){
            this.attendancePersonJobNo=res;
            this.allowClickSubmit=true;
          },
          handleSelectionChange(val,a){
            this.multipleSelectionRows=val;
            this.multipleSelectionKeys=[];
            for(let i=0;i<this.multipleSelectionRows.length;i++){
              this.multipleSelectionKeys.push(this.multipleSelectionRows[i].id);
            }
          },
          selectionChange(list){
            this.multipleSelectionRows=list;
            this.multipleSelectionKeys=[];
            for(let i=0;i<list.length;i++){
              this.multipleSelectionKeys.push(list[i].id);
            }
          },
          submitBomData(){ //提交
            if(this.multipleSelectionRows.length<=0){
              this.$message.warning("请选择数据");
              return;
            }
            if(!this.searchForm.startTime || !this.searchForm.endTime){
              this.$message.warning("请选择时间范围");
              return;
            }
            let now = new Date();
            if (now.getHours() < 7) {
              now.setDate(now.getDate() - 1);
            }
            for(let item of this.multipleSelectionRows){
              if(dateFormat(new Date(item.settleTime)).substr(0, 10) != dateFormat(now).substr(0, 10)){
                this.$message.warning("请检查所选择数据有已超过最晚提交时间数据，已超过最晚提交时间数据需联系线路进行补提交！");
                return;
              }
            }
            if(
              dateFormat(new Date(this.searchForm.startTime)).substr(0, 10) != dateFormat(now).substr(0, 10) ||
              dateFormat(new Date(this.searchForm.endTime)).substr(0, 10) != dateFormat(now).substr(0, 10)

            ){
              this.$message.warning("请检查选择的时间范围不正确！");
              return;
            }
            this.loadingShow = true;
            this.allowClickSubmit = false;
            let startSettleDate=moment(this.searchForm.startTime).format('YYYY-MM-DD')+" 02:00:00";
            let endSettleDate=moment(this.searchForm.endTime).add(1,'days').format("YYYY-MM-DD")+" 01:59:59";
            this.dialogVisible = false;
            bomSubmitTvmData({attendancePersonJobNo:this.attendancePersonJobNo,
              params:JSON.stringify(this.multipleSelectionRows),
              startTime:startSettleDate,endTime:endSettleDate,
              isLong:true
            }).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功");
                this.search();
                this.$parent.search();
                this.loadingShow = false;
                this.dialogVisible = false;
              }else{
                this.loadingShow = false;
                this.$message.error("操作失败");
              }
            })
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.search()
            done()
          },
        },
      mounted() {
        //this.search();
      }
    }
</script>
<style lang="scss">

</style>
